package com.yz.jdd.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.CountDownTimer;
import android.util.AttributeSet;
import android.widget.Button;


import com.yz.jdd.R;

import java.lang.ref.WeakReference;

/**
 * Created by lkf on 2017/1/7.
 */

public class TimerButton extends Button {

    private String afterText = "重发";
    private int ms = 60000;

    CountDownTimer timer;

    public TimerButton(Context context) {
        super(context);
    }

    public TimerButton(Context context, AttributeSet attrs) {

        this(context, attrs,0);

    }

    public TimerButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.timerbutton);


        afterText = typedArray.getString(R.styleable.timerbutton_afterText);

        ms = typedArray.getInt(R.styleable.timerbutton_ms,ms);

        typedArray.recycle();
    }
    public void init(String afterText, int ms){


        this.afterText = afterText;
        this.ms = ms;

    }

    public void startTimer(){

        TimerButton.this.setEnabled(false);

       new TimeCounter(ms+1000,1000,new WeakReference<Button>(this) ).start();

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
